<?php
/**
 * @author: arian
 * @email: arian@dwebsite.com
 * @on: 06/23/08
 *
 */
 
 Zend_Loader::loadClass("BaseDBRows");
 Zend_Loader::loadClass("Module");
 class Modules extends BaseDBRows
 {
 	protected function setUp()
 	{
 		$this->_tableName = Module::_tablename;
 	}	
	
	public function getUserTypeModule($userType= 0,$parentModuleId = 0){
		Zend_Loader::loadClass("UserTypeModulePermission");
		
		$query2 = new QueryCreator();
		$query2->addSelect(" m.id , m.code , m.value , m.description , m.display_order,m.visibility ");
		$query2->addFrom( Module::_tablename . " as m ");
		$query2->addJoin(" INNER JOIN " . UserTypeModulePermission::_tablename . " as utmp ON m.id = utmp.module_id ");
		if($userType)
			$query2->addWhere(" utmp.user_type_id  = ". $userType );
			
		$query2->addWhere(" m.parent_module_id = ". $parentModuleId );
		$query2->addWhere(" m.visibility != 0");
		
		$query2->addOrderBy(" display_order ASC");
		$db = DBCon::instance();
		//echo $query2->createSQL();die();
		return $db->executeQuery($query2->createSQL());
	}
	
	
	public function getAllowedModulesArray($userId = 0 , $userTypeId = 0, $parentModuleId = 0){
		
		Zend_Loader::loadClass("UserModulePermission");
		Zend_Loader::loadClass("UserTypeModulePermission");
		
		
		$query1 = new QueryCreator();
		$query1->addSelect(" m.id , m.code , m.value , m.description ,m.display_order ");
		$query1->addFrom( Module::_tablename . " as m ");
		$query1->addJoin(" INNER JOIN " . UserModulePermission::_tablename . " as ump ON m.id = ump.module_id ");
		$query1->addWhere(" ump.user_id = ". $userId );
		$query1->addWhere(" m.parent_module_id = ". $parentModuleId );
		$query1->addWhere(" m.visibility != 0");
		
		$query2 = new QueryCreator();
		$query2->addSelect(" m.id , m.code , m.value , m.description , m.display_order");
		$query2->addFrom( Module::_tablename . " as m ");
		$query2->addJoin(" INNER JOIN " . UserTypeModulePermission::_tablename . " as utmp ON m.id = utmp.module_id ");
		$query2->addWhere(" utmp.user_type_id  = ". $userTypeId );
		$query2->addWhere(" m.parent_module_id = ". $parentModuleId );
		$query2->addWhere(" m.visibility != 0");
		
		$query2->addOrderBy(" display_order ASC");
		$db = DBCon::instance();
		
		//echo $query1->createSQL() . " UNION " . $query2->createSQL();
		return $db->executeQuery($query1->createSQL() . " UNION " . $query2->createSQL());
	}
	
	public function getModulesByParentModuleId($parentModuleId = 0 ){
		$query1 = new QueryCreator();
		$query1->addSelect(" * ");
		$query1->addFrom( Module::_tablename . " as m ");
		$query1->addWhere(" m.parent_module_id = ". $parentModuleId );
		$db = DBCon::instance();
		return $db->executeQuery($query1->createSQL());
	}
	
 }
?>